<?php


namespace app\api\controller;

use app\api\model\Base as bs;
use think\Db;
use think\Request;

class Teachercourse extends Base
{
    public function teacher()
    {
        $data = Db::name('user')->field('id,username')->field(['id'=>'key'])->where('permissions',3)->where('status',1)
                ->select();
        $total = bs::total('user',['permissions'=>3]);
        $this->apiReturn(200, '', $data, $total);
    }

    public function Teachercourse()
    {
        $head = $this->request = Request::instance()->header();
        if(substr($head['token'], -1)==4){
            $this->apiReturn(101,'敢越过权限，你胆子蛮大嘛');
        }
        if (Request::instance()->method() == 'GET') {
            $id = I('id');
            $page = input('current','1','intval');
            $page_size = input('pageSize',10,'intval');
            $like = I("username|mobile|student_number|email|score|course_name");

            $data = Db::name('course')
                ->alias('a')
                ->join('user_course b', 'a.id = b.course_id')
                ->join('user c', 'b.uid = c.id')
                ->field('a.course_name,c.username,c.student_number,c.mobile,c.email,b.score')
                ->field(['a.id' => 'key', 'c.id' => 'user_id'])
                ->where('a.status', 1)
                ->where('a.teacher_id', $id)
                ->where('c.id','not null')
                ->where('c.username', 'like', '%'.$like['username'].'%')
                ->where('c.student_number', 'like', '%'.$like['student_number'].'%')
                ->where('a.course_name', 'like', '%'.$like['course_name'].'%')
                ->page($page,$page_size);
            if(!empty($like['mobile'])){
                $data->where('mobile', 'like', '%'.$like['mobile'].'%');
            }
            if(!empty($like['email'])){
                $data->where('email', 'like', '%'.$like['email'].'%');
            }
            if(!empty($like['score'])){
                $data->where('score', 'like', '%'.$like['score'].'%');
            }
            $data = $data->select();
            foreach ($data as $k => $v) {
                if ($v['user_id'] == null) {
                    unset($v);
                }
            }
            $total = Db::name('course')
                ->alias('a')
                ->join('user_course b', 'a.id = b.course_id')
                ->join('user c', 'b.uid = c.id')
                ->where('a.status', 1)
                ->where('a.teacher_id', $id)
                ->where('c.id','not null')
                ->count();
            $this->apiReturn(200, '', $data, $total);
        }

        if (Request::instance()->method() == 'PUT') {
            $value = I('id|score|course_id');
            foreach ($value as $k => $v) {
                if (!$v) {
                    $this->apiReturn(303, 'lack ' . $k);
                }
            }
            $flag = Db::name('user_course')->where(
                ['course_id'=>$value['course_id'],'uid'=>$value['id']])
                ->update([
                    'score' => $value['score']
                ]);
            $this->apiReturn(200, '打分成功', $flag);
        }
    }

}